package com.ibm.android;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.method.PasswordTransformationMethod;
import android.view.MotionEvent;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;

import com.ibm.android.camera.CameraManager;
import com.ibm.android.camera.ICamera;
import com.ibm.android.camera.ImageCamera;
import com.ibm.android.format.BusinessData;

public class ScanActivity extends Activity {
	ScanResultHandler mScanResultHandler;
	EditText product_name;
	EditText provider_name_et;
	EditText description_et;
	EditText provider_email_et;
	EditText price_et;
	EditText currency_et;
	Button cancel_button;
	EditText user_id;
	EditText pwd;
	HistoryStore mHistoryStore;

	int logCount = 0;

	// Button scan_button;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Window window = getWindow();
		window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

		mHistoryStore = new HistoryStore(getApplicationContext());
	}

	private boolean loginApproved(String user_id, String pwd) {
		if (user_id.length() > 0 && pwd.length() > 0) {
			return true;
		}
		return false;
	}

	@Override
	protected void onPause() {
		super.onPause();
		if (CameraManager.get() != null)
			CameraManager.get().closeDriver();
	}

	@Override
	protected void onResume() {
		super.onResume();
		setContentView(R.layout.login);

		user_id = (EditText) findViewById(R.id.user_id_et);
		pwd = (EditText) findViewById(R.id.pwd_et);
		pwd.setTransformationMethod(PasswordTransformationMethod.getInstance());

		Button login_button = (Button) findViewById(R.id.enter_button);
		login_button.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				checkLogin();
				return true;
			}

		});

		Button cancel_login = (Button) findViewById(R.id.cancel_login_btn);
		cancel_login.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				finish();
				return true;
			}
		});
	}

	private void checkLogin() {

		if (loginApproved(user_id.getText().toString(), pwd.getText()
				.toString())) {
			initMain();
		} else {
			// logCount++;
			// if(logCount>3){
			// finish();
			// }
			final AlertDialog logDialog = new AlertDialog.Builder(this)
					.create();
			setContentView(R.layout.login);
			logDialog.setTitle("Login Error");
			logDialog.setMessage("Invalid login");
			logDialog.setButton("OK", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {

					onResume();
				}

			});
			logDialog.show();

		}

	}

	public void setValidData(final BusinessData bd) {
		setContentView(R.layout.product_info);

		product_name = (EditText) findViewById(R.id.product_name_et);
		product_name.setText(bd.getProductName());
		product_name.setEnabled(false);

		description_et = (EditText) findViewById(R.id.description_et);
		description_et.setText(bd.getDescription());
		description_et.setEnabled(false);

		provider_name_et = (EditText) findViewById(R.id.provider_name_et);
		provider_name_et.setText(bd.getVendorName());
		provider_name_et.setEnabled(false);

		provider_email_et = (EditText) findViewById(R.id.provider_emial_et);
		provider_email_et.setText(bd.getEmail());
		provider_email_et.setEnabled(false);

		price_et = (EditText) findViewById(R.id.price_et);
		price_et.setText("" + bd.getPaymentAmount());
		price_et.setEnabled(false);

		currency_et = (EditText) findViewById(R.id.currency_et);
		currency_et.setText(bd.getCurrency());
		currency_et.setEnabled(false);

		cancel_button = (Button) findViewById(R.id.cancel_button);
		cancel_button.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				initMain();
				return true;
			}
		});
		final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
		Button buy_button = (Button) findViewById(R.id.buyproduct_button);
		buy_button.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				alertDialog.setTitle("Transaction information");
				alertDialog.setMessage("Transaction succeed");
				alertDialog.setButton("OK",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
								mHistoryStore.addItem(bd);
								initMain();
							}

						});
				alertDialog.show();
				return true;
			}
		});
	}

	void initMain() {
		final ScanActivity sa = this;
		setContentView(R.layout.main);
		Button scan_button = (Button) findViewById(R.id.btn_scan);
		scan_button.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub

				// ICamera httpCamera = new
				// HttpCamera("http://192.168.184.1:8080/",
				// new Point(320, 240));
				Bitmap image = BitmapFactory.decodeResource(getResources(),
						R.drawable.test_qr_code2);
				ICamera camera = new ImageCamera(image);
				setContentView(R.layout.scan);
				SurfaceView preview = (SurfaceView) findViewById(R.id.preview_view);
				CameraManager.init(sa, camera);
				CameraManager.get().openDriver(preview.getHolder());
				CameraManager.get().startPreview();

				mScanResultHandler = new ScanResultHandler(sa);

				return true;
			}

		});

		Button exit_button = (Button) findViewById(R.id.exit_button_id);
		exit_button.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				finish();
				return true;
			}
		});

		Button historic_button = (Button) findViewById(R.id.historic_button);
		historic_button.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {
				new HistoryView(sa, mHistoryStore);
				return true;
			}
		});
	}

	public HistoryStore history() {
		return mHistoryStore;
	}
}

